<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon" />
      <span class="title">{{goodsId?"同类商品":"人气推荐"}}推荐</span>
    </div>
    <!-- 此处使用改造后的xtx-carousel.vue -->
    <XtxCarousel :sliders="list" style="height:380px" auto-play />
  </div>
</template>

<script>
import { findRelGoods } from '@/api/goods'
import { ref } from 'vue'

export default {
  // 同类推荐，猜你喜欢
  name: 'GoodsRelevant',
  props: {
    goodsId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // A1-获取商品同类信息
    const list = ref([])
    findRelGoods(props.goodsId).then(ret => {
      // A1-1 把原始的16条数据分成4页
      // A1-2 每页的条数
      const pageSize = 4
      // A1-3 计算一共有多少页,用数据的总条数/每页有几条
      const pageNum = Math.ceil(ret.result.length / pageSize)
      // A1-4 循环遍历总页数次
      for (let i = 1; i <= pageNum; i++) {
        // A1-5 获取每页的数据,把总数据切割成四个数组,每个数组有四条数据
        const perpage = ret.result.slice((i - 1) * pageSize, i * pageSize)

        // A1-6 把切割成的四组数据,放在list中
        list.value.push(perpage)
      }
    })
    return { list }
  }
}
</script>

<style scoped lang='less'>
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  :deep(.xtx-carousel) {
    height: 380px;
    .carousel {
      &-indicator {
        bottom: 30px;
        span {
          &.active {
            background: @xtxColor;
          }
        }
      }
      &-btn {
        top: 110px;
        opacity: 1;
        background: rgba(0, 0, 0, 0);
        color: #ddd;
        i {
          font-size: 30px;
        }
      }
    }
  }
  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: '';
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}
</style>
